package com.ty.recyclerviewdemo

import android.content.Context
import android.graphics.Color
import android.util.Log
import androidx.recyclerview.widget.RecyclerView
import com.ty.recyclerviewdemo.TestAutoDataAdapter.ItemClickListener
import android.view.ViewGroup
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import com.ty.recyclerviewdemo.R
import android.view.View.OnLongClickListener
import android.widget.TextView
import java.util.HashSet

/**
 * Created by flisar on 03.03.2017.
 */
class TestAutoDataAdapter(private val mContext: Context, private val mDataSize: Int) :
    RecyclerView.Adapter<TestAutoDataAdapter.ViewHolder>() {
    private var mClickListener: ItemClickListener? = null
    val selection: HashSet<Int>
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val view =
            LayoutInflater.from(mContext).inflate(R.layout.test_cell, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.tvText.text = position.toString()
        if (selection.contains(position)) holder.tvText.setBackgroundColor(Color.parseColor("#00bfa8")) else holder.tvText.setBackgroundColor(
            Color.WHITE
        )
    }

    override fun getItemCount(): Int {
        return mDataSize
    }

    // ----------------------
    // Selection
    // ----------------------
    fun toggleSelection(pos: Int) {
        if (selection.contains(pos)) selection.remove(pos) else selection.add(pos)
        notifyItemChanged(pos)
    }

    fun select(pos: Int, selected: Boolean) {
        if (selected) selection.add(pos) else selection.remove(pos)
        notifyItemChanged(pos)
    }

    fun selectRange(start: Int, end: Int, selected: Boolean) {
        for (i in start..end) {
            if (selected) selection.add(i) else selection.remove(i)
        }
        notifyItemRangeChanged(start, end - start + 1)
    }

    fun deselectAll() {
        // this is not beautiful...
        selection.clear()
        notifyDataSetChanged()
    }

    fun selectAll() {
        for (i in 0 until mDataSize) selection.add(i)
        notifyDataSetChanged()
    }

    val countSelected: Int
        get() = selection.size

    // ----------------------
    // Click Listener
    // ----------------------
    fun setClickListener(itemClickListener: ItemClickListener?) {
        mClickListener = itemClickListener
    }

    interface ItemClickListener {
        fun onItemClick(view: View?, position: Int)
        fun onItemLongClick(view: View?, position: Int): Boolean
        fun onItemTouch(view:View?,position: Int):Boolean
    }

    // ----------------------
    // ViewHolder
    // ----------------------
    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
        View.OnClickListener, OnLongClickListener,View.OnTouchListener {
        var tvText: TextView
        override fun onClick(view: View) {
            if (mClickListener != null) mClickListener!!.onItemClick(view, adapterPosition)
        }

        override fun onLongClick(view: View): Boolean {
            return if (mClickListener != null) mClickListener!!.onItemLongClick(
                view,
                adapterPosition
            ) else false
        }

        init {
            tvText = itemView.findViewById<View>(R.id.tvText) as TextView
            itemView.setOnClickListener(this)
            itemView.setOnLongClickListener(this)
            itemView.setOnTouchListener(this)
        }

        override fun onTouch(v: View?, event: MotionEvent): Boolean {
            return if (mClickListener != null){
                if(event.action==MotionEvent.ACTION_DOWN) {
                    mClickListener!!.onItemTouch(v, adapterPosition)
                }else
                    false
            }else {
                false
            }

        }
    }

    init {
        selection = HashSet()
    }
}